import requests
import re
from common.read_acountinfo import ReadConfig

class GetLoginToken:
    username=""
    password=""
    def getlogintoken(self):
        session = requests.session()
        # 打开单点登录页面
        LoginSinglePage_Resp =session.get(ReadConfig().get_http('baseurl1'))
        # 获取登录页面隐藏的session
        sessionpattern = '<input\s+?type="hidden"\s+?name="_csrf"\s+?value="(.*?)"\s+?/>'
        token = re.findall(sessionpattern, LoginSinglePage_Resp.text)
        print(token)
        # 提交登录
        LoginSingle_Resp = session.post(ReadConfig().get_http('baseurl1'),
                                 {"username":self.username,
                                  "password":self.password,
                                  "_csrf": token})
        # 获取登录账号的token
        Resp_managetoken = session.get(ReadConfig().get_http('baseurl2'))
        logintoken = "Bearer" + " " + Resp_managetoken.text
        # 退出登录
        session.get(ReadConfig().get_http('baseurl3'))
        return logintoken


class school1001_Logintoken(GetLoginToken):
    username = ReadConfig().get_account('school_username1')
    password = ReadConfig().get_account('school_password1')
class school1002_Logintoken(GetLoginToken):
    username = ReadConfig().get_account('school_username2')
    password = ReadConfig().get_account('school_password2')
class county1003_Logintoken(GetLoginToken):
    username = ReadConfig().get_account('county_username1')
    password = ReadConfig().get_account('county_password1')
class county1004_Logintoken(GetLoginToken):
    username = ReadConfig().get_account('county_username2')
    password = ReadConfig().get_account('county_password2')
class city1005_Logintoken(GetLoginToken):
    username = ReadConfig().get_account('city_username')
    password = ReadConfig().get_account('city_password')
class root_Logintoken(GetLoginToken):
    username = ReadConfig().get_account('root_username')
    password = ReadConfig().get_account('root_password')
if __name__=="__main__":
    # print(school1001_Logintoken().getlogintoken())
    # print(school1002_Logintoken().getlogintoken())
    # print(county1003_Logintoken().getlogintoken())
    # print(county1004_Logintoken().getlogintoken())
    # print(city1005_Logintoken().getlogintoken())
    print(root_Logintoken().getlogintoken())